package com.web.service.Impl;

import com.web.constant.UserConstants;
import com.web.dao.UserInfoDao;
import com.web.dao.UserPermissionDao;
import com.web.exception.BusinessException;
import com.web.pojo.domain.Permission;
import com.web.pojo.domain.UserInfo;
import com.web.pojo.domain.UserPermission;
import com.web.pojo.vo.Menu;
import com.web.service.UserServer;
import com.web.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wws
 * @Date: 2021/2/22 15:49
 * describe:
 */
@Service
public class UserServerImpl implements UserServer {

    @Autowired
    UserInfoDao userDao;

    @Autowired
    UserPermissionDao userPermissionDao;

    /**
     * 添加子用户
     * @param userInfo 用户信息
     * @return 结果
     */
    @Override
    public Integer insertUserInfo(UserInfo userInfo) {
        if (userInfo.getPid() == 0) {
            userInfo.setPids("0");
        } else {
            UserInfo parent = userDao.selectUserById(userInfo.getPid());
            // 如果父节点不为正常状态,则不允许新增子节点
            if (UserConstants.ADDRESS_DISABLE.equals(parent.getDelFlag())) {
                throw new BusinessException("用户停用，不允许新增");
            }
            userInfo.setPids(parent.getPids() + "," + userInfo.getPid());
        }

        userDao.insertUser(userInfo);
        return insertUserPermissions(userInfo);
    }

    /**
     * 添加用户权限
     * @param userInfo 添加用户权限
     * @return 结果
     */
    public Integer insertUserPermissions(UserInfo userInfo)
    {
        List<UserPermission> list = new ArrayList<UserPermission>();
        for (Integer permissionId : userInfo.getPermissionIds()) {
            UserPermission userPermission = new UserPermission();
            userPermission.setUserId(userInfo.getUserId());
            userPermission.setPermissionId(permissionId);
            list.add(userPermission);
        }
        return userPermissionDao.insertUserPermissions(list);

    }

    /**
     * 判断用户名是否唯一
     * @param username 用户名
     * @return 结果
     */
    @Override
    public String checkUserNameUnique(String username) {
        Integer count = userDao.checkUserNameUnique(username);
        return count > 0 ? UserConstants.NOT_UNIQUE : UserConstants.UNIQUE;
    }

    /**
     * 修改用户信息
     * @param userInfo 用户信息
     * @return 结果
     */
    @Override
    public Integer updateUserInfo(UserInfo userInfo) {
        return userDao.updateUserInfo(userInfo);
    }

    /**
     * 删除用户信息
     * @param userIds 用户id
     * @return 结果
     */
    @Override
    public Integer deleteUserInfo(List<Integer> userIds) {
        String username = SecurityUtils.getUsername();
        userDao.deleteUserInfo(username,userIds);
        return userPermissionDao.deleteUserPermission(userIds);
    }

    /**
     * 修改子用户权限
     * @param userInfo 信息
     * @return 结果
     */
    @Override
    public Integer updateAuthority(UserInfo userInfo) {
        userPermissionDao.deleteUserPermissionById(userInfo.getUserId());
        return insertUserPermissions(userInfo);
    }

    /**
     * 查询子用户信息
     * @param str 查询条件
     * @return 结果
     */
    @Override
    public List<UserInfo> selectUserList(String str) {
        Integer userId = SecurityUtils.getUserId();
        return userDao.selectUserList(userId,str);
    }

    /**
     * 查询子用户详细信息
     * @param userInfo 查询条件
     * @return 结果
     */
    @Override
    public UserInfo selectUserInfo(Integer userInfo) {
        return userDao.selectUserInfo(userInfo);
    }

    /**
     * 查看权限列表
     * @return 查看权限列表
     */
    @Override
    public List<Permission> selectAuthorityList(Integer userId) {
        return userPermissionDao.selectAuthorityList(userId);
    }

    @Override
    public UserInfo selectUserInfoByUsername(String username) {
        return userDao.selectUserInfoByUsername(username);
    }


}
